<?php
require_once 'Abstract.php';
/**
 * @package   Zend_Db
 * @copyright Agora Production 2002-2006 (http://agoraproduction.com)
 * @license   New BSD Licence
 * @author    David Coallier <davidc@agoraproduction.com>
 */
class Kpl_Db_Adapter_Ingres extends Kpl_Db_Adapter_Abstract
{
    const ZEND_DB_INGRES_DBTYPE = 'ingres';
    /**
     * Constructor
     */
    public function __construct($config)
    {
        $config['type'] = self::ZEND_DB_INGRES_DBTYPE;
        parent::__construct($config);
    }
    /**
     * Returns the column descriptions for a table.
     *
     * @access public
     * @param string $table The table name to lookup
     * @return array
     */

    public function describeTable($table, $schema = null)
    {
    	$this->_connection->loadModule('Manager', null, true);
     	$tableFields = $this->_connection->manager->listTableFields($table);
		$this->_connection->loadModule('Reverse');
		
		$ZendDbDescr = array();
		
		foreach($tableFields as $position => $field)
		{
			
     		$MDB2_descr = current($this->_connection->getTableFieldDefinition($table, $field));
     		
     		$ZendDbDescr[$field] = array(
        		'SCHEMA_NAME'      => $schema,
        		'TABLE_NAME'       => $table,
        		'COLUMN_NAME'      => $field,
        		'COLUMN_POSITION'  => $position + 1,
        		'DATA_TYPE'        => $MDB2_descr['type'],
        		'DEFAULT'          => $MDB2_descr['default'],
        		'NULLABLE'         => $MDB2_descr['notnull'],
        		'LENGTH'           => $MDB2_descr['length'],
        		'SCALE'            => null,
        		'PRECISION'        => null,
        		'UNSIGNED'         => null,
        		'PRIMARY'          => ($field == id),
        		'PRIMARY_POSITION' => 1,
        		'IDENTITY' => false
     		);         			 
		}
		
		return $ZendDbDescr;
    }
    
    public function getServerVersion()
    {
    	return INGRES_API_VERSION;
    }

    public function isConnected()
    {
    	return true;
    }
    
    public function limit($sql, $count, $offset = 0)
    {
		return $this->_connection->_modifyQuery($sql,false,$count,$offset);
    }

    public function nextSequenceId($sequenceName)
    {
	$sql = 'SELECT ' . $sequenceName . '.NEXTVAL';
	return $this->_connection->queryOne($sql, 'integer', ini_get('ingres.array_index_start'));
    }

    
}
